#ifndef CYDEVICE_H
#define CYDEVICE_H

#include <Windows.h>
#include <QtCore>
#include <QQueue>
#include "CyAPI.h"
#include "camera_config.h"

struct ImageData {
	uchar* data;
	int bitwidth;
	int height;
	int width;
};


class CyDevice : public QObject {
	Q_OBJECT

public:
	CyDevice();
	~CyDevice();

	bool open(HANDLE hnd);
	void close();
	void resize(int maxSize, int queueSize);
	void enableReceive(bool enable);
	bool receiving();
	void wait();
	bool empty();
	ImageData read();

	void setCommandSet(Camera::Command command);
	void setResolution(int width);
	void setBitwidth(int bitwidth);
	void setAnalog(int analog);
	void setExposure(int exposure);

signals:
	void received();

private:
	bool sendControlCode(int code);
	bool sendRequestCode(int code, QVector<uchar> buf);
	void receive(LONG sizePerXfer, int xferQueueSize, int timeOut);

	Camera::Command command;
	int height;
	int width;
	int bitwidth;
	bool isReceiving;

	int bufferIndex;
	QVector<ImageData> imageBuffer;
	QQueue<ImageData> imageQueue;

	QReadWriteLock mutex;

	CCyUSBDevice* device;
	CCyUSBEndPoint* dataInEndPoint;
	CCyControlEndPoint* controlEndPoint;
};

#endif
